package me.spring.cloud.common.components.response.service;

import java.util.ArrayList;
import java.util.List;
import me.spring.cloud.common.components.response.code.ErrorCode;
import me.spring.cloud.common.components.rpc.response.CommonResponse;
import me.spring.cloud.common.components.rpc.response.header.CommonRespHeader;
import me.spring.cloud.common.components.rpc.response.header.RespHeader;
import me.spring.cloud.common.components.util.enums.CodedEnum;

/**
 * @author luffy
 * @date 2020-04-17 22:14
 */
public abstract class CommonRespService implements ResponseService {

  @Override
  public <T> CommonResponse<T> success() {
    return new CommonResponse<>(createHeader(ErrorCode.OK), null);
  }

  @Override
  public <T> CommonResponse<T> success(T t) {
    return new CommonResponse<>(createHeader(ErrorCode.OK), t);
  }

  @Override
  public <T> CommonResponse<T> error() {
    return new CommonResponse<>(createHeader(ErrorCode.FAIL), null);
  }

  @Override
  public <T> CommonResponse<T> error(CodedEnum codedEnum) {
    return new CommonResponse<>(createHeader(codedEnum), null);
  }

  @Override
  public <T> CommonResponse<T> error(Object code) {
    return new CommonResponse<>(createError(code), null);
  }

  @Override
  public <T> CommonResponse<T> errorBody(T body) {
    return new CommonResponse<>(createError(ErrorCode.FAIL.getCode()), body);
  }

  @Override
  public <T> CommonResponse<T> error(String message) {
    return new CommonResponse<>(createErrorMessage(message), null);
  }

  @Override
  public <T> CommonResponse<T> error(Object code, String message) {
    return new CommonResponse<>(createError(code, message), null);
  }

  public RespHeader createHeader(CodedEnum codedEnum) {
    return new CommonRespHeader(codedEnum.getCode(), codedEnum.getDesc());
  }

  public RespHeader createError(Object code) {
    return new CommonRespHeader(code, ErrorCode.FAIL.getDesc());
  }

  public RespHeader createError(Object code, String message) {
    return new CommonRespHeader(code, message);
  }

  public RespHeader createErrorMessage(String message) {
    return new CommonRespHeader(ErrorCode.FAIL.getCode(), message);
  }

  @Override
  public List<String> getIgnoreUrls(){
    return new ArrayList<>();
  }
}
